﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.Devices;

namespace AgentTest
{
    /// <summary>
    /// 物理硬件参数
    /// </summary>
     [Serializable]
    class PhysicalParameter
    {
        PerformanceCounter cpu;
        ComputerInfo cinf;
        private  ulong DiskSize=0;
        private ManagementClass mc;
        public PhysicalParameter()
        {
            cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            cinf = new ComputerInfo();
             mc = new ManagementClass("Win32_DiskDrive");
        }
        public void Monitor(Action<float, ulong> callback)
        {
            while (true)
            {
                var percentage = cpu.NextValue();
                var mem = cinf.AvailablePhysicalMemory;
                callback(percentage, mem);

                System.Threading.Thread.Sleep(1000);
            }
        }

        public void Dispose()
        {
            cpu.Dispose();
        }

        public ulong GetPhysicalMemSize()
        {
            return cinf.TotalPhysicalMemory-cinf.AvailablePhysicalMemory;
        }
        public ulong GetSizeOfDiskSize()
        {
            ManagementObjectCollection managementObjectCollection = mc.GetInstances();
            foreach (var m in managementObjectCollection)
            {
                DiskSize += Convert.ToUInt64(m.Properties["Size"].Value);
            }
            return DiskSize;
        }

        /// <summary>
        /// 获取硬盘上所有的盘符空间信息列表
        /// </summary>
        /// <returns></returns>
        private List<HardDiskPartition> GetDiskListInfo()
        {
            List<HardDiskPartition> list = null;
            //指定分区的容量信息
            try
            {
                SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");

                ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);

                ManagementObjectCollection diskcollection = searcher.Get();
                if (diskcollection != null && diskcollection.Count > 0)
                {
                    list = new List<HardDiskPartition>();
                    HardDiskPartition harddisk = null;
                    foreach (ManagementObject disk in searcher.Get())
                    {
                        int nType = Convert.ToInt32(disk["DriveType"]);
                        if (nType != Convert.ToInt32(DriveType.Fixed))
                        {
                            continue;
                        }
                        else
                        {
                            harddisk = new HardDiskPartition();
                            harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]);
                            harddisk.SumSpace = Convert.ToDouble(disk["Size"]);
                            harddisk.PartitionName = disk["DeviceID"].ToString();
                            list.Add(harddisk);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
            return list;
        }

    }


}
